//
//  CorgiVisitorLoginView.swift
//  weibo
//
//  Created by Corgi on 17/3/20.
//  Copyright © 2017年 cocoaHangTou. All rights reserved.
//

import UIKit
import SnapKit

protocol CorgiVisitorLoginViewDelegate {
    
    func userWillRegister(visitorView: CorgiVisitorLoginView)
    
    func userWillLogin(visitor: CorgiVisitorLoginView)
}

class CorgiVisitorLoginView: UIView {

    var delegate: CorgiVisitorLoginViewDelegate?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //对外接口,判断加载什么图片和文字
    func updateUI(title: String, imageName: String, isHomeVC: Bool = false) {
        
        self.largeHouse.isHidden = !isHomeVC
        
        self.coverView.isHidden = !isHomeVC
        
        self.circleView.image = UIImage(named: imageName)
        
        self.tipLabel.text = title
        
        if isHomeVC {
            
            startAnimation()
        }
    }
    
    private func startAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        
        animation.duration = 15
        
        animation.repeatCount = 0
        
        animation.isRemovedOnCompletion = false
        
        circleView.layer.add(animation, forKey: nil)
    }
    
    //布局
    private func setupUI() {
    
        addSubview(circleView)
        addSubview(coverView)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        circleView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-80)
        }
        
        coverView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        
        largeHouse.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        
        tipLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(circleView)
            make.top.equalTo(circleView.snp.bottom).offset(16)
        }
        
        tipLabel.preferredMaxLayoutWidth = 230
        
        registerBtn.snp.makeConstraints { (make) in
            make.leading.equalTo(circleView).offset(-40)
            make.top.equalTo(tipLabel.snp.bottom).offset(16)
            make.width.equalTo(100)
        }
        
        loginBtn.snp.makeConstraints { (make) in
            make.trailing.equalTo(circleView).offset(40)
            make.top.equalTo(tipLabel.snp.bottom).offset(16)
            make.width.equalTo(100)
        }
        
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
        
        registerBtn.addTarget(self, action: #selector(registerClick), for: .touchUpInside)
        
        loginBtn.addTarget(self, action: #selector(loginClick), for: .touchUpInside)
        
        
    }
    
    @objc private func registerClick() {
        
        delegate?.userWillRegister(visitorView: self)
        
    }
    
    @objc private func loginClick() {
        
        delegate?.userWillLogin(visitor: self)
        
    }
    
    //懒加载子视图
    private lazy var circleView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    
    private lazy var largeHouse: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))

    private lazy var coverView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var tipLabel: UILabel = UILabel(title: "关注一些人,看看有什么惊喜", textColor: UIColor.darkGray, fontSize: 14)
    
    private lazy var loginBtn: UIButton = UIButton(title: "登录", textColor: UIColor.darkGray, fontSize: 15, backImageName: "common_button_white")
    
    private lazy var registerBtn: UIButton = UIButton(title: "注册", textColor: UIColor.orange, fontSize: 15, backImageName: "common_button_white")
    
    
}
